package com.ax.api.route.base.drugstore;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.ax.api.route.util.RouteConfig;
import com.netflix.config.ConfigurationManager;

@RestController
@RequestMapping("/drugstore")
public class DrugStoreRoute {
	 @Autowired
	 LoadBalancerClient loadBalancerClient;
	 @Autowired
	 RestTemplate restTemplate;
	 
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping("/sys/drugstore")
	String drugstore() {
	   String url = geturl("/sys/drugstore");
	   return restTemplate.getForObject(url, String.class);
	}
	
	/**
	 * queryDrugStore
	 * remote_base
	 * 药房查询(模块:药店/公司信息)
	 */ 
	@GetMapping("/queryDrugStore")
	public String queryDrugclass(@RequestParam Map<String,Object> map) {
		String value = "?drugstoreId={drugstoreId}";
		String url = geturl("/sys/drugstore/queryDrugStore") + value;
		return restTemplate.getForObject(url, String.class,map);
}

	/**
	 * add
	 * remote_base
	 * 药房增加(模块:药房管理)*跳转增加页面
	 */   
	@GetMapping("/add/drugstore")
	public String add(String pId){
		String value = "/pId="+pId;
	    String url = geturl("/sys/drugstore/add") + value;
		return restTemplate.getForObject(url, String.class,pId);
	}
	
	/**
	 * insertBaseDrugstore
	 * remote_base
	 * 药房保存(模块:药房管理)
	 */ 
	@GetMapping("/insertBaseDrugstore")
	public String insertBaseDrugClass(@RequestParam Map<String,Object> map) {
		String value = "?drugstoreId={drugstoreId}&drugstoreName={drugstoreName}&firstLetter={firstLetter}&legalPersonName={legalPersonName}&legalPersonId={legalPersonId}&regCapital={regCapital}&areaCode={areaCode}&areaName={areaName}&phone={phone}&address={address}&openDate={openDate}&longitude={longitude}&latitude={latitude}&account={account}";
		value += "&bank={bank}&creditCode={creditCode}&introduction={introduction}&remarks={remarks}&parentDrugstoreId={parentDrugstoreId}&drugstoreType={drugstoreType}&flag={flag}";
		String url = geturl("/sys/drugstore/insertBaseDrugstore") + value;
		return restTemplate.getForObject(url, String.class,map);
  }
  
   /**
	 * edit
	 * remote_base
	 * 药房修改(模块:药房管理)*跳转修改页面
	 */   
	@GetMapping("/edit/drugstore")
	public String edit(String id){
		String value = "/" + id;
	    String url = geturl("/sys/drugstore/edit") + value;
		return restTemplate.getForObject(url, String.class,id);
	}
	
	/**
	 * updateBaseDrugstore
	 * remote_base
	 * 药房修改(模块:药房管理)
	 */ 
	@GetMapping("/updateBaseDrugstore")
	public String updateBaseDrugstore(@RequestParam Map<String,Object> map) {
		String value = "?drugstoreId={drugstoreId}&drugstoreName={drugstoreName}&firstLetter={firstLetter}&legalPersonName={legalPersonName}&legalPersonId={legalPersonId}&regCapital={regCapital}&areaCode={areaCode}&areaName={areaName}&phone={phone}&address={address}&openDate={openDate}&longitude={longitude}&latitude={latitude}&account={account}";
		value += "&bank={bank}&creditCode={creditCode}&introduction={introduction}&remarks={remarks}&parentDrugstoreId={parentDrugstoreId}&drugstoreType={drugstoreType}&flag={flag}";
		String url = geturl("/sys/drugstore/updateBaseDrugstore") + value;
		return restTemplate.getForObject(url, String.class,map);
  }
	
	@GetMapping("/test")
	public String updateBaseDrugstorse(@RequestParam Map<String,Object> map) {
		String value = "?pt={pt}";
		String url = geturl("/sys/drugstore/getTest") + value;
		return restTemplate.getForObject("http://127.0.0.1:3101/sys/drugstore/getTest?pt={pt}", String.class,map);
  }
	
	private String geturl(String path){
		 ServiceInstance serviceInstance = loadBalancerClient.choose(RouteConfig.BASE_ROUTE);
		 StringBuffer bf = new StringBuffer("http://");
		 bf.append(serviceInstance.getHost());
		 bf.append(":");
		 bf.append(serviceInstance.getPort());
		 bf.append(path);
		 return bf.toString();
	   }
}
